/**
 * 
 */
package com.ims.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author Achal B.I
 *
 */
public class propertyManager {
	private static propertyManager instance;

	private static Properties prop = new Properties();

	public static String getProp(String key) {

		if (prop == null || prop.isEmpty()) {
			prop = loadProperty.load("picklistvalues.properties");
		}
		return prop.getProperty(key);
	}
	public List<CodeValueData> getListFromProp(String key) {
		
		String currProp = getProp(key);
		ArrayList<CodeValueData> typeList = new ArrayList<CodeValueData>();
		ArrayList<String> recList = new ArrayList<String>();
		
		Scanner scanRec = new Scanner(currProp).useDelimiter(",");
		while (scanRec.hasNext()) {
			String currRec = scanRec.next();
			recList.add(currRec);
		}
		for (String currRec : recList) {
			Scanner scanEachRec = new Scanner(currRec).useDelimiter(":");
			CodeValueData eachType = new CodeValueData();
			String currCode = null;
			String currVal = null;
			while (scanEachRec.hasNext()) {
				if (currCode == null)
					currCode = scanEachRec.next();
				else
					currVal = scanEachRec.next();
			}
			// LOG.info("eachRecVal:code:" + currCode
			// + ":currVal:" + currVal);
			eachType.setCode(currCode);
			eachType.setValue(currVal);
			typeList.add(eachType);
		}
		return typeList;
	}
	public static propertyManager getInstance() {
		if (instance == null) {
			instance = new propertyManager();
		}
		return instance;
	}
	public String getValueFromProp(String key, String code) {
		String value = "";
		String currProp = getProp(key);
		ArrayList<String> recList = new ArrayList<String>();
		
		Scanner scanRec = new Scanner(currProp).useDelimiter(",");
		while (scanRec.hasNext()) {
			String currRec = scanRec.next();
			recList.add(currRec);
		}
		for (String currRec : recList) {
			Scanner scanEachRec = new Scanner(currRec).useDelimiter(":");
			CodeValueData eachType = new CodeValueData();
			String currCode = null;
			String currVal = null;
			while (scanEachRec.hasNext()) {
				if(scanEachRec.next().equals(code)){
					value = scanEachRec.next();
					return value;
				}
			}
		
		}
		return value;
	}
}
